﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControlPresupuesto
{
    public partial class FrmRegistrosContablesINFOR_Revisar : Form
    {
        public FrmRegistrosContablesINFOR_Revisar()
        {
            InitializeComponent();
        }

        private void FrmRegistrosContablesINFOR_Revisar_Load(object sender, EventArgs e)
        {   
            // TODO: esta línea de código carga datos en la tabla 'controlPresupuestoDataSet.spRegistroINFORCabecera' Puede moverla o quitarla según sea necesario.
            this.spRegistroINFORCabeceraTableAdapter.Fill(this.controlPresupuestoDataSet.spRegistroINFORCabecera);

            //-- Blanquear Campos
            Utils.get().blanquearCampos(this);

            this.PeriodoBuscadorAWIL_Leave(null, null);
        }

        private void PeriodoBuscadorAWIL_Leave(object sender, EventArgs e)
        {
            if (this.PeriodoBuscadorAWIL.AWIL_Texto.Trim() == string.Empty || !this.PeriodoBuscadorAWIL.AWIL_ValorCorrecto)
            {
                this.controlPresupuestoDataSet.spRegistroINFORDetalle.Rows.Clear();
                this.ObservacionesTextBox.Text = string.Empty;
                return;
            }

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                this.ObservacionesTextBox.Text = (this.PeriodoBuscadorAWIL.AWIL_DataRowCurrent == null ? string.Empty : this.PeriodoBuscadorAWIL.AWIL_DataRowCurrent["Observaciones"].ToString());

                this.spRegistroINFORDetalleTableAdapter.FillBy(
                    this.controlPresupuestoDataSet.spRegistroINFORDetalle,
                    "SelectRegistrosPorPeriodo",
                    null,
                    null,
                    null,
                    null,
                    null,
                    this.PeriodoBuscadorAWIL.AWIL_Texto.Trim(),
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null
                    );
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
            }
            Cursor.Current = Cursors.Default;
        }

        #region Eventos asociados a los botones del BindingNavigator

        private void exportar_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try { Utils.get().exportar(this.spRegistroINFORDetalleDataGridView); }
            catch (Exception ex) { MessageException.show(ex); }
            Cursor.Current = Cursors.Default;
        }

        private void actualizar_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ManageForms.reloadForm(this);
            Cursor.Current = Cursors.Default;
        }

        private void filter_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            BindingSource bs = ((BindingNavigator)((ToolStripButton)sender).GetCurrentParent()).BindingSource;
            try
            {
                bs.RemoveFilter();
                this.toolStripTextBox1.Text = string.Empty;
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
                bs.CancelEdit();
            }
            Cursor.Current = Cursors.Default;
        }

        private void filterText_TextChanged(object sender, EventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            BindingSource bs = ((BindingNavigator)((ToolStripTextBox)sender).GetCurrentParent()).BindingSource;
            DataTable table = ((DataSet)bs.DataSource).Tables[bs.DataMember];
            ToolStripTextBox tb = (ToolStripTextBox)sender;
            try
            {
                String filtro = "";
                foreach (DataColumn cl in table.Columns)
                {
                    filtro += "ISNULL(" + cl.ColumnName.ToString() + ",'')+' '+";
                }
                filtro = filtro.Substring(0, filtro.Length - 5);
                filtro += " like'%" + tb.Text + "%'";
                //MessageBox.Show(filtro);
                bs.Filter = filtro;
            }
            catch (Exception ex)
            {
                MessageException.show(ex);
                bs.CancelEdit();
            }
            Cursor.Current = Cursors.Default;
        }
        #endregion

    }
}
